package com.cq.hd.admin.controller;

import com.cq.hd.admin.annotation.RequiresPermissionsDesc;
import com.cq.hd.admin.biz.OrderBiz;
import com.cq.hd.common.enums.biz.FinancialAccountTypeEnum;
import com.cq.hd.common.enums.biz.WithdrawalTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.TbWithdrawalBillApi;
import com.cq.hd.member.api.dto.AdminWithdrawalApplyPageDto;
import com.cq.hd.member.api.dto.AdminWithdrawalBillPaymentVoucherDto;
import com.cq.hd.member.api.dto.WithdrawalCheckBillSaveDto;
import com.cq.hd.member.api.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Api(tags = "商户提现审核相关接口(结算池)")
@Slf4j
@RestController
@RequestMapping("/withdrawalBill")
public class TbWithdrawalBillController extends BaseController{

    @Autowired
    private TbWithdrawalBillApi withdrawalBillApi;

    @Autowired
    private OrderBiz orderBiz;

    @ApiOperation(value = "(结算池商户补贴)获取累计申请金额，累计已提现金额")
    @RequiresPermissionsDesc(menu = {"财务管理", "结算池商户补贴申请管理"}, button = "累计查询")
    @GetMapping("/settlementPool/getSubsidyAmt")
    public Result<AdminWithdrawalAmtVo> getSubsidyAmt(AdminWithdrawalApplyPageDto adminWithdrawalApplyPageDto) {
        adminWithdrawalApplyPageDto.setWithdrawalType(WithdrawalTypeEnum.MERCHANT_SUBSIDY.getValue());
        adminWithdrawalApplyPageDto.setMerchantFinancialAccountType(FinancialAccountTypeEnum.SELF.getValue());
        return withdrawalBillApi.getWithdrawalAmt(adminWithdrawalApplyPageDto);
    }

    @ApiOperation(value = "(结算池商户本金)获取累计申请金额，累计已提现金额")
    @RequiresPermissionsDesc(menu = {"财务管理", "结算池商户本金申请管理"}, button = "累计查询")
    @GetMapping("/settlementPool/getAmt")
    public Result<AdminWithdrawalAmtVo> getAmt(AdminWithdrawalApplyPageDto adminWithdrawalApplyPageDto) {
        adminWithdrawalApplyPageDto.setWithdrawalType(WithdrawalTypeEnum.MERCHANT_AMT.getValue());
        adminWithdrawalApplyPageDto.setMerchantFinancialAccountType(FinancialAccountTypeEnum.SELF.getValue());
        return withdrawalBillApi.getWithdrawalAmt(adminWithdrawalApplyPageDto);
    }

    @ApiOperation(value = "结算池商户补贴提现审核列表")
    @RequiresPermissionsDesc(menu = {"财务管理", "结算池商户补贴提现审核"}, button = "查询")
    @GetMapping("/settlementPool/subsidyPage")
    public Result<Page<AdminWithdrawalApplyPageVo>> subsidyPage(AdminWithdrawalApplyPageDto adminWithdrawalApplyPageDto) {
        formatWithdrawalApplyPageDto(adminWithdrawalApplyPageDto);
        adminWithdrawalApplyPageDto.setMerchantFinancialAccountType(FinancialAccountTypeEnum.PLATFORM.getValue()); // 结算池商户
        adminWithdrawalApplyPageDto.setWithdrawalType(WithdrawalTypeEnum.MERCHANT_SUBSIDY.getValue()); // 补贴
        return withdrawalBillApi.pageAdminWithdrawalBill(adminWithdrawalApplyPageDto);
    }

    @ApiOperation(value = "结算池商户本金提现审核列表")
    @RequiresPermissionsDesc(menu = {"财务管理", "结算池商户本金提现审核"}, button = "查询")
    @GetMapping("/settlementPool/amtPage")
    public Result<Page<AdminWithdrawalApplyPageVo>> amtPage(AdminWithdrawalApplyPageDto adminWithdrawalApplyPageDto) {
        formatWithdrawalApplyPageDto(adminWithdrawalApplyPageDto);
        adminWithdrawalApplyPageDto.setMerchantFinancialAccountType(FinancialAccountTypeEnum.PLATFORM.getValue()); // 结算池商户
        adminWithdrawalApplyPageDto.setWithdrawalType(WithdrawalTypeEnum.MERCHANT_AMT.getValue()); // 本金
        return withdrawalBillApi.pageAdminWithdrawalBill(adminWithdrawalApplyPageDto);
    }

    /**
     * 商户ID/简称
     * 商家ID/简称
     * 两个关键词搜索处理
     * @param adminWithdrawalApplyPageDto
     */
    private void formatWithdrawalApplyPageDto(AdminWithdrawalApplyPageDto adminWithdrawalApplyPageDto) {
        // 商户ID/简称
        String merchantKeyword = adminWithdrawalApplyPageDto.getMerchantKeyword();
        if (StringUtils.isMerchantNo(merchantKeyword)) {
            adminWithdrawalApplyPageDto.setMerchantNo(merchantKeyword);
        } else if (StringUtils.isNotBlank(merchantKeyword)) {
            adminWithdrawalApplyPageDto.setNickName(merchantKeyword);
        }

        // 商家ID/简称
        String businessKeyword = adminWithdrawalApplyPageDto.getBusinessKeyword();
        if (StringUtils.isBusinessNo(businessKeyword)) {
            adminWithdrawalApplyPageDto.setBusinessNo(businessKeyword);
        } else if (StringUtils.isNotBlank(businessKeyword)) {
            adminWithdrawalApplyPageDto.setBusinessShortName(businessKeyword);
        }
    }

    @ApiOperation(value = "打款（上传打款凭证）")
    @RequiresPermissionsDesc(menu = {"财务管理", "结算池商户提现审核"}, button = "打款")
    @PostMapping("/settlementPool/uploadPaymentVoucher")
    public Result<Boolean> uploadPaymentVoucher(@RequestBody AdminWithdrawalBillPaymentVoucherDto adminWithdrawalBillPaymentVoucherDto) {
        return withdrawalBillApi.uploadPaymentVoucher(adminWithdrawalBillPaymentVoucherDto);
    }

    @ApiOperation(value = "获取打款凭证")
    @RequiresPermissionsDesc(menu = {"财务管理", "结算池商户提现审核"}, button = "获取打款凭证")
    @GetMapping("/settlementPool/getPaymentVoucher")
    public Result<TbWithdrawalBillPaymentVoucherVo> getPaymentVoucher(@RequestParam("withdrawalBillId") Long withdrawalBillId) {
        return withdrawalBillApi.getPaymentVoucher(withdrawalBillId);
    }

    @ApiOperation(value = "核算初始化")
    @RequiresPermissionsDesc(menu = {"财务管理", "核算"}, button = "核算")
    @GetMapping("/check/init")
    public Result<WithdrawalCheckBillInitVo> initCheck(@RequestParam("withdrawalId") Long withdrawalId) {
        return withdrawalBillApi.initCheck(withdrawalId);
    }

    @ApiOperation(value = "核算信息")
    @RequiresPermissionsDesc(menu = {"财务管理", "核算信息"}, button = "核算信息")
    @GetMapping("/check/info")
    public Result<WithdrawalCheckBillInitVo> getCheckBill(@RequestParam("checkBillId") Long checkBillId) {
        return withdrawalBillApi.checkInfo(checkBillId);
    }

    @ApiOperation(value = "核算保存")
    @RequiresPermissionsDesc(menu = {"财务管理", "核算保存"}, button = "核算保存")
    @PostMapping("/check/save")
    public Result<Boolean> saveCheckBill(@RequestBody @Valid WithdrawalCheckBillSaveDto checkBillSaveDto) {
        return withdrawalBillApi.saveCheckBill(checkBillSaveDto);
    }

    @ApiOperation(value = "核算预览")
    @RequiresPermissionsDesc(menu = {"财务管理", "核算预览"}, button = "核算预览")
    @GetMapping("/check/pre")
    public Result<WithdrawalCheckBillPreVo> preCheckBill(@RequestParam("checkBillId") Long checkBillId) {
        return withdrawalBillApi.preCheckBill(checkBillId);
    }

    @ApiOperation(value = "下载订单明细")
    @RequiresPermissionsDesc(menu = {"财务管理", "下载订单明细"}, button = "下载订单明细")
    @GetMapping("/exportOrderExcel")
    public void exportOrderExcel(@RequestParam("withdrawalId") Long withdrawalId, HttpServletResponse response) {
        orderBiz.exportOrderByWithdrawalId(withdrawalId, response);
    }
}
